package com.tg.demo.web;

import com.github.pagehelper.PageInfo;
import com.tg.demo.common.BarView;
import com.tg.demo.common.ConstantUtil;
import com.tg.demo.common.Page;
import com.tg.demo.common.Result;
import com.tg.demo.entity.Study;
import com.tg.demo.mapper.StudyMapper;
import com.tg.demo.service.StudyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * StudyController
 *
 * @author yuanjin
 * @date 2019/3/29
 */
@Controller
@Slf4j
@RequestMapping("/study")
public class StudyController {

    @Resource
    private StudyService studyService;
    @Resource
    private StudyMapper studyMapper;

    /**
     * 跳转到列表页
     *
     * @param model the model
     * @return string string
     */
    @RequestMapping(value = "/gotoListPage", method = {RequestMethod.GET, RequestMethod.POST})
    public String gotoListPage(Model model) {
        return "/study/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Page list(Page page, Study study) {
        PageInfo<Study> info = studyService.getListByPage(study, page.getPage(), page.getPageSize());
        Page result = new Page();
        result.setTotal(Integer.parseInt(info.getTotal() + ""));
        result.setRows(info.getList());
        return result;
    }

    @RequestMapping(value = "/DA", method = {RequestMethod.GET, RequestMethod.POST})
    public String da(Model model) {
        return "/study/view";
    }

    @RequestMapping("/averageScore")
    @ResponseBody
    public Result averageScore() {
        List<Double> items = studyMapper.getItemsByAverageScore();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        String[] arr = {"小于60分", "60-69分", "70-79分", "80-89分", "90分及以上"};
        for (String item : arr) {
            keys.add(item);
            Double target = null;
            if (item.equals("小于60分")) {
                target = 60.00;
            } else if (item.equals("60-69分")) {
                target = 70.00;
            } else if (item.equals("70-79分")) {
                target = 80.00;
            } else if (item.equals("80-89分")) {
                target = 90.00;
            } else if (item.equals("90分及以上")) {
                target = 100.00;
            }
            values.add(getCount(items, target));
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    @RequestMapping("/counselorScore")
    @ResponseBody
    public Result counselorScore() {
        List<Double> items = studyMapper.getItemsByCounselorScore();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        String[] arr = {"小于60分", "60-69分", "70-79分", "80-89分", "90分及以上"};
        for (String item : arr) {
            keys.add(item);
            Double target = null;
            if (item.equals("小于60分")) {
                target = 60.00;
            } else if (item.equals("60-69分")) {
                target = 70.00;
            } else if (item.equals("70-79分")) {
                target = 80.00;
            } else if (item.equals("80-89分")) {
                target = 90.00;
            } else if (item.equals("90分及以上")) {
                target = 100.00;
            }
            values.add(getCount(items, target));
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    @RequestMapping("/studyTime")
    @ResponseBody
    public Result studyTime() {
        List<Integer> items = studyMapper.getItemsByStudyTime();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            if (i < 5) {
                keys.add(i + "小时");
            } else {
                keys.add(i + "小时及以上");
            }
            int count = 0;
            for (Integer ele : items) {
                if (i == ele) {
                    count++;
                }
            }
            values.add(count);
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    @RequestMapping("/readBookCount")
    @ResponseBody
    public Result readBookCount() {
        List<Integer> items = studyMapper.getItemsByReadBookCount();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        String[] arr = {"3本以下", "3-5本", "6-10本", "10本以上"};
        for (int i = 0; i < arr.length; i++) {
            keys.add(arr[i]);
            int count = 0;
            for (Integer ele : items) {
                if (i == 0 && ele < 3) {
                    count++;
                } else if (i == 1 && ele >= 3 && ele < 6) {
                    count++;
                } else if (i == 2 && ele >= 6 && ele < 11) {
                    count++;
                } else if (i == 3 && ele >= 11) {
                    count++;
                }
            }
            values.add(count);
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    @RequestMapping("/failedCount")
    @ResponseBody
    public Result failedCount() {
        List<Integer> items = studyMapper.getItemsByFailedCount();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            if (i < 6) {
                keys.add(i + "次");
            } else {
                keys.add(i + "次及以上");
            }
            int count = 0;
            for (Integer ele : items) {
                if (i == ele) {
                    count++;
                }
            }
            values.add(count);
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    @RequestMapping("/awardCount")
    @ResponseBody
    public Result awardCount() {
        List<Integer> items = studyMapper.getItemsByAwardCount();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            if (i < 6) {
                keys.add(i + "次");
            } else {
                keys.add(i + "次及以上");
            }
            int count = 0;
            for (Integer ele : items) {
                if (i == ele) {
                    count++;
                }
            }
            values.add(count);
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    @RequestMapping("/truancyCount")
    @ResponseBody
    public Result truancyCount() {
        List<Integer> items = studyMapper.getItemsByTruancyCount();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        String[] arr = {"5次以下", "5-10次", "10-15次", "15次以上"};
        for (int i = 0; i < arr.length; i++) {
            keys.add(arr[i]);
            int count = 0;
            for (Integer ele : items) {
                if (i == 0 && ele < 5) {
                    count++;
                } else if (i == 1 && ele >= 5 && ele < 11) {
                    count++;
                } else if (i == 2 && ele >= 11 && ele < 16) {
                    count++;
                } else if (i == 3 && ele >= 16) {
                    count++;
                }
            }
            values.add(count);
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    @RequestMapping("/competitionCount")
    @ResponseBody
    public Result competitionCount() {
        List<Integer> items = studyMapper.getItemsByCompetitionCount();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        String[] arr = {"0次", "1次", "2次", "3次", "4次", "5次以上"};
        for (int i = 0; i < arr.length; i++) {
            keys.add(arr[i]);
            int count = 0;
            for (Integer ele : items) {
                if (i == 0 && ele == 0) {
                    count++;
                } else if (i == 1 && ele == 1) {
                    count++;
                } else if (i == 2 && ele == 2) {
                    count++;
                } else if (i == 3 && ele == 3) {
                    count++;
                } else if (i == 4 && ele == 4) {
                    count++;
                } else if (i == 5 && ele >= 5) {
                    count++;
                }
            }
            values.add(count);
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }


    private Integer getCount(List<Double> items, Double target) {
        int count = 0;
        for (Double ele : items) {
            if (target == 60 && ele < 60) {
                count++;
            } else if (target == 70 && ele >= 60 && ele < 70) {
                count++;
            } else if (target == 80 && ele >= 70 && ele < 80) {
                count++;
            } else if (target == 90 && ele >= 80 && ele < 90) {
                count++;
            } else if (target == 100 && ele >= 90) {
                count++;
            }
        }
        return count;
    }
}
